/*
 * @Author: your name
 * @Date: 2022-01-26 10:18:31
 * @LastEditTime: 2022-04-02 11:04:37
 * @LastEditors: your name
 * @Description: 新建或编辑学生信息页面
 * @FilePath: \web\src\router\adminUserPage\adminChild\studentCreate.jsx
 */

import React from "react";
import { Form, Input, Button, message } from "antd";
import { useNavigate, useLocation } from 'react-router-dom';
import request from '../../../network';
import AdminBox from "../../../components/adminBox";

const StudentCreate = () => {
  const navigate = useNavigate();
  const {state} = useLocation();
  const title = state ? "编辑学生" : "新建学生";
  const [form] = Form.useForm();
  const onFinish = async (values) => {
    values.th = values.id.substr(0,4);
    console.log('values',values);
    if (state) {
      await request.put('/admin?type=3',{id: state.id, password : values.password});
      message.success("修改成功!", 1.5);
    } else {
      await request.post('/admin/s', values);
      message.success("添加成功!", 1.5);
    }
    navigate("/adminUserPage/studentList");
  };
  return (
    <div id="studentCreate">
      <AdminBox title={title}>
        <div className="admin-context">
          <Form
            form={form}
            name="basic"
            labelCol={{ span: 10 }}
            wrapperCol={{ span: 16 }}
            initialValues={{ remember: true }}
            onFinish={onFinish}
            autoComplete="off"
          >
            <Form.Item
              label="学号"
              name="id"
              initialValue= {state ? state.id:""}
              rules={[{ required: true }]}
            >
              <Input disabled = {state ? true : false} />
            </Form.Item>
            <Form.Item
              label="学院"
              name="academy"
              initialValue= {state ? state.academy:""}
              rules={[{ required: true }]}
            >
              <Input disabled = {state ? true : false} />
            </Form.Item>
            <Form.Item
              label="专业"
              name="major"
              initialValue= {state ? state.major:""}
              rules={[{ required: true }]}
            >
              <Input disabled = {state ? true : false} />
            </Form.Item>
            <Form.Item
              label="班级"
              name="_class"
              initialValue= {state ? state._class:""}
              rules={[{ required: true }]}
            >
              <Input disabled = {state ? true : false} />
            </Form.Item>
            <Form.Item
              label="姓名"
              name="username"
              initialValue= {state ? state.username:""}
              rules={[{ required: true }]}
            >
              <Input />
            </Form.Item>
            <Form.Item
              label="密码"
              name="password"
              rules={[{ required: true }]}
            >
              <Input />
            </Form.Item>
            <Form.Item wrapperCol={{ offset: 10, span: 16 }}>
              <Button type="primary" htmlType="submit">
                保存
              </Button>
            </Form.Item>
          </Form>
        </div>
      </AdminBox>
    </div>
  );
};

export default StudentCreate;
